
public class Writer extends Thread {

    private final Database database;

    public Writer(Database database) {
        this.database = database;
    }

    @Override
    public void run() {
        while (true) {
            
            try {
                database.startWrite();
            } catch (InterruptedException ex) {
            } 
           
            int writeTime = (int) (Math.random() * 10);
            System.out.println("Writer writes on database for " + writeTime + " seconds");

            try {
                Thread.sleep(writeTime * 1000);
            } catch (InterruptedException e) {
            }

            System.out.println("Writer leaves database");
            database.endWrite();
        }
    }
}
